package com.sz.jinzheng.controller;

import com.sz.jinzheng.cilent.UserCilent;
import com.sz.jinzheng.model.PageBean;
import com.sz.jinzheng.model.Result;
import com.sz.jinzheng.model.StaffExmination;
import com.sz.jinzheng.model.Task;
import com.sz.jinzheng.service.StaffExaminationService;
import com.sz.jinzheng.service.TaskService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/inspector")
public class TaskController {
    @Autowired
    private TaskService taskService;
    @Autowired
    private UserCilent userCilent;
    @Autowired
    private StaffExaminationService staffExaminationService;
    //多条件查询任务
    @RequestMapping("/getTaskByExam")
    public Result getTaskByExam(@RequestBody Task task, @RequestParam("index") int index, @RequestParam("size")int size) {
        PageBean<Task> taskByExam = taskService.getTaskByExam(task, index, size);
        return new Result(200, "成功", taskByExam);
    }

    /**
     * 领取任务后在审核人员信息表中增加审批记录
     *
     * @param task
     * @return
     */
    @RequestMapping("/pickTask")
    public Result pickTask(@RequestBody Task task) {
        //通过token拿到uid
        Integer uid = userCilent.getUid(task.getToken());
        task.setUserId(uid);
        //首先查询看是否参与过这个任务参与过不给领
        StaffExmination exmination = new StaffExmination();
        exmination.setTaskId(task.getId());
        exmination.setStaffMemberId(task.getUserId());
        Integer exmination1 = staffExaminationService.getStaffExminationByUid(exmination);
        if (exmination1>0){
            //如果大于0表示已经领取过
            return new Result(400,"你已经参与过该任务无法领取");
        }else {
            if (task.getStatus() != 8) {
                task.setStatus(task.getStatus() + 1);
            }

            int i = taskService.updateTaskStatus(task);
            if (i > 0) {
                return new Result(200, "领取成功", null);
            }
        }

        return new Result(409, "领取失败请稍后重试", null);
    }
}
